/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.activity;

import android.content.Context;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.ouertech.android.agm.lib.ui.base.MenuTxt;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseTopActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.utils.UtilOuer;


import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author Wang
 * @date 16/2/22
 * @desc 意见反馈
 */
public class FeedbackActivity extends BaseTopActivity {
    //内容
    @Bind(R.id.setting_feedback_content)
    EditText mEtContent;

    @Override
    protected void initTop() {
        setTitle(R.string.feedback_title);
        setNavigation(R.drawable.common_ic_left_nav);

        //提交
        MenuTxt txt = new MenuTxt.MenuTxtBuilder(this)
            .setTitle(R.string.feedback_commit)
            .setListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    feedback();
                    return false;
                }
            }).build();

        addMenu(txt);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_feedback);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

        mEtContent.setFocusable(true);
        mEtContent.setFocusableInTouchMode(true);
        mEtContent.requestFocus();

        OuerApplication.mOuerFuture.delay(500,
                new OuerFutureListener(this){

                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        InputMethodManager inputMethodManager =
                                (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        inputMethodManager.showSoftInput(mEtContent, 0);
                    }

                });
    }

    /**
     * 提交意见
     */
    private void feedback() {
        String content = mEtContent.getText().toString().trim();

        if(UtilString.isBlank(content)) {
            UtilOuer.toast(this, R.string.feedback_content_empty);
            mEtContent.requestFocus();
            return;
        }

        // 发送意见反馈
        AgnettyFuture future = OuerApplication.mOuerFuture.feedback(content, null,
                new OuerFutureListener(this){

                    public void onStart(AgnettyResult result) {
                        setWaitingDialog(true);
                    }

                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        setWaitingDialog(false);

                        UtilOuer.toast(FeedbackActivity.this, R.string.feedback_success);
                        finish();
                    }

                    public void onNetUnavaiable(AgnettyResult result) {
                        super.onNetUnavaiable(result);
                        setWaitingDialog(false);
                    }

                    @Override
                    public void onException(AgnettyResult result) {
                        super.onException(result);
                        setWaitingDialog(false);
                        UtilOuer.toast(FeedbackActivity.this, R.string.feedback_failure);
                    }
                });

        attachDestroyFutures(future);
    }
}
